public class 环形链表II {

      class ListNode {
          int val;
          ListNode next;
          ListNode(int x) {
              val = x;
              next = null;
          }
      }
    public ListNode detectCycle(ListNode head) {
        // 快慢指针
        ListNode slow = head;
        ListNode fast = head;
        // 没有节点或者只有一个节点
        if (fast == null || fast.next == null) {
            return null;
        }
        // 判断是否有环
        do {
            slow = slow.next;
            fast = fast.next.next;
        } while (fast != null && fast.next != null && fast != slow);
        // 无环
        if (slow != fast) {
            return null;
        }
        // 同时开始找
        ListNode cur = head;
        while (cur != fast) {
            cur = cur.next;
            fast = fast.next;
        }
        return cur;
    }
}
